[id].vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <div class="admin--ir-form">
  3. <div v-if="isLoading" class="admin--loading">
  4. 데이터를 불러오는 중...
  5. </div>
  6. <form v-else @submit.prevent="handleSubmit" class="admin--form">
  7. <!-- 댓글허용 -->
  8. <div class="admin--form-group">
  9. <label class="admin--form-label">댓글허용</label>
  10. <div class="admin--checkbox-group">
  11. <label class="admin--checkbox-label">
  12. <input v-model="formData.allow_comment" type="checkbox">
  13. <span>댓글 허용</span>
  14. </label>
  15. </div>
  16. </div>
  17. <!-- 공지 -->
  18. <div class="admin--form-group">
  19. <label class="admin--form-label">공지</label>
  20. <div class="admin--checkbox-group">
  21. <label class="admin--checkbox-label">
  22. <input v-model="formData.is_notice" type="checkbox">
  23. <span>공지글로 등록</span>
  24. </label>
  25. </div>
  26. </div>
  27. <!-- 이름 -->
  28. <div class="admin--form-group">
  29. <label class="admin--form-label">이름 <span class="admin--required">*</span></label>
  30. <input
  31. v-model="formData.name"
  32. type="text"
  33. class="admin--form-input"
  34. placeholder="이름을 입력하세요"
  35. required
  36. >
  37. </div>
  38. <!-- 이메일 -->
  39. <div class="admin--form-group">
  40. <label class="admin--form-label">이메일 <span class="admin--required">*</span></label>
  41. <input
  42. v-model="formData.email"
  43. type="email"
  44. class="admin--form-input"
  45. placeholder="이메일을 입력하세요"
  46. required
  47. >
  48. </div>
  49. <!-- URL -->
  50. <div class="admin--form-group">
  51. <label class="admin--form-label">URL</label>
  52. <input
  53. v-model="formData.url"
  54. type="url"
  55. class="admin--form-input"
  56. placeholder="https://example.com"
  57. >
  58. </div>
  59. <!-- 제목 -->
  60. <div class="admin--form-group">
  61. <label class="admin--form-label">제목 <span class="admin--required">*</span></label>
  62. <input
  63. v-model="formData.title"
  64. type="text"
  65. class="admin--form-input"
  66. placeholder="제목을 입력하세요"
  67. required
  68. >
  69. </div>
  70. <!-- 내용 -->
  71. <div class="admin--form-group">
  72. <label class="admin--form-label">내용 <span class="admin--required">*</span></label>
  73. <SunEditor v-model="formData.content" />
  74. </div>
  75. <!-- 기존 첨부파일 -->
  76. <div v-if="existingFiles.length > 0" class="admin--form-group">
  77. <label class="admin--form-label">기존 첨부파일</label>
  78. <div class="admin--file-list">
  79. <div v-for="(file, index) in existingFiles" :key="'existing-' + index" class="admin--file-item">
  80. <a :href="file.url" target="_blank" class="admin--file-name">{{ file.name }}</a>
  81. <span class="admin--file-size">({{ formatFileSize(file.size) }})</span>
  82. <button type="button" class="admin--btn-remove-file" @click="removeExistingFile(index)">
  83. 삭제
  84. </button>
  85. </div>
  86. </div>
  87. </div>
  88. <!-- 새 파일첨부 -->
  89. <div class="admin--form-group">
  90. <label class="admin--form-label">파일첨부</label>
  91. <div v-if="attachedFiles.length > 0" class="admin--file-list">
  92. <div v-for="(file, index) in attachedFiles" :key="'new-' + index" class="admin--file-item">
  93. <span class="admin--file-name">{{ file.name }}</span>
  94. <span class="admin--file-size">({{ formatFileSize(file.size) }})</span>
  95. <button type="button" class="admin--btn-remove-file" @click="removeFile(index)">
  96. 삭제
  97. </button>
  98. </div>
  99. </div>
  100. <input
  101. ref="fileInput"
  102. type="file"
  103. multiple
  104. class="admin--form-file-hidden"
  105. @change="handleFileAdd"
  106. >
  107. <button type="button" class="admin--btn admin--btn-secondary" @click="triggerFileInput">
  108. 파일 추가
  109. </button>
  110. </div>
  111. <!-- 버튼 영역 -->
  112. <div class="admin--form-actions">
  113. <button
  114. type="submit"
  115. class="admin--btn admin--btn-primary"
  116. :disabled="isSaving"
  117. >
  118. {{ isSaving ? '저장 중...' : '확인' }}
  119. </button>
  120. <button
  121. type="button"
  122. class="admin--btn admin--btn-secondary"
  123. @click="goToList"
  124. >
  125. 목록
  126. </button>
  127. </div>
  128. <!-- 성공/에러 메시지 -->
  129. <div v-if="successMessage" class="admin--alert admin--alert-success">
  130. {{ successMessage }}
  131. </div>
  132. <div v-if="errorMessage" class="admin--alert admin--alert-error">
  133. {{ errorMessage }}
  134. </div>
  135. </form>
  136. </div>
  137. </template>
  138. <script setup>
  139. import { ref, onMounted } from 'vue'
  140. import { useRoute, useRouter } from 'vue-router'
  141. import SunEditor from '~/components/admin/SunEditor.vue'
  142. definePageMeta({
  143. layout: 'admin',
  144. middleware: ['auth']
  145. })
  146. const route = useRoute()
  147. const router = useRouter()
  148. const { get, put, upload } = useApi()
  149. const isLoading = ref(true)
  150. const isSaving = ref(false)
  151. const successMessage = ref('')
  152. const errorMessage = ref('')
  153. const attachedFiles = ref([])
  154. const existingFiles = ref([])
  155. const fileInput = ref(null)
  156. const formData = ref({
  157. allow_comment: false,
  158. is_notice: false,
  159. name: '',
  160. email: '',
  161. url: '',
  162. title: '',
  163. content: '',
  164. file_urls: []
  165. })
  166. const loadIR = async () => {
  167. isLoading.value = true
  168. const id = route.params.id
  169. const { data, error } = await get(`/board/ir/${id}`)
  170. console.log('[IREdit] 데이터 로드:', { data, error })
  171. if (data?.success && data?.data) {
  172. const ir = data.data
  173. formData.value = {
  174. allow_comment: ir.allow_comment || false,
  175. is_notice: ir.is_notice || false,
  176. name: ir.name || '',
  177. email: ir.email || '',
  178. url: ir.url || '',
  179. title: ir.title || '',
  180. content: ir.content || '',
  181. file_urls: ir.file_urls || []
  182. }
  183. existingFiles.value = ir.file_urls || []
  184. console.log('[IREdit] 로드 성공')
  185. }
  186. isLoading.value = false
  187. }
  188. const triggerFileInput = () => {
  189. fileInput.value?.click()
  190. }
  191. const handleFileAdd = (event) => {
  192. const files = Array.from(event.target.files)
  193. attachedFiles.value.push(...files)
  194. event.target.value = ''
  195. }
  196. const removeFile = (index) => {
  197. attachedFiles.value.splice(index, 1)
  198. }
  199. const removeExistingFile = (index) => {
  200. existingFiles.value.splice(index, 1)
  201. }
  202. const formatFileSize = (bytes) => {
  203. if (bytes === 0) return '0 Bytes'
  204. const k = 1024
  205. const sizes = ['Bytes', 'KB', 'MB', 'GB']
  206. const i = Math.floor(Math.log(bytes) / Math.log(k))
  207. return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
  208. }
  209. const handleSubmit = async () => {
  210. successMessage.value = ''
  211. errorMessage.value = ''
  212. if (!formData.value.title) {
  213. errorMessage.value = '제목을 입력하세요.'
  214. return
  215. }
  216. if (!formData.value.content) {
  217. errorMessage.value = '내용을 입력하세요.'
  218. return
  219. }
  220. isSaving.value = true
  221. try {
  222. let fileUrls = [...existingFiles.value]
  223. // 새 파일 업로드
  224. if (attachedFiles.value.length > 0) {
  225. for (const file of attachedFiles.value) {
  226. const formDataFile = new FormData()
  227. formDataFile.append('file', file)
  228. const { data: uploadData, error: uploadError } = await upload('/upload/file', formDataFile)
  229. if (uploadError) {
  230. errorMessage.value = `파일 업로드에 실패했습니다: ${file.name}`
  231. isSaving.value = false
  232. return
  233. }
  234. fileUrls.push({
  235. name: file.name,
  236. url: uploadData.url,
  237. size: file.size
  238. })
  239. }
  240. }
  241. const submitData = {
  242. ...formData.value,
  243. file_urls: fileUrls
  244. }
  245. const id = route.params.id
  246. const { data, error } = await put(`/board/ir/${id}`, submitData)
  247. if (error) {
  248. errorMessage.value = error.message || '수정에 실패했습니다.'
  249. } else {
  250. successMessage.value = 'IR 자료가 수정되었습니다.'
  251. setTimeout(() => {
  252. router.push('/admin/board/ir')
  253. }, 1000)
  254. }
  255. } catch (error) {
  256. errorMessage.value = '서버 오류가 발생했습니다.'
  257. console.error('Save error:', error)
  258. } finally {
  259. isSaving.value = false
  260. }
  261. }
  262. const goToList = () => {
  263. router.push('/admin/board/ir')
  264. }
  265. onMounted(() => {
  266. loadIR()
  267. })
  268. </script>